package com.e3.sysmg.security.server;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.e3.core.utils.XMLUtils;

public class UserHelper {
	
	private static String CONF_FILE = "/conf/e3admin.xml";
	
	private static boolean isInited = false;
	
	private static Long localCorpId;
	
	private static String localCorpCode;
	
	private static String localCorpName;
	
	private static Long superAdminId;
	
	private static String superAdmincode;
	
	private static String superAdminName;
	
	private static String superPassword;
	
	public static Long getLocalCorpId(){
		init();
		return localCorpId;
	}
	
	public static String getLocalCorpCode(){
		init();
		return localCorpCode;
	}
	
	public static String getLocalCorpName(){
		init();
		return localCorpName;
	}
	
	public static Long getSuperAdminId(){
		init();
		return superAdminId;
	}
	
	public static String getSuperAdminCode(){
		init();
		return superAdmincode;
	}
	
	public static String getSuperAdminName(){
		init();
		return superAdminName;
	}
	
	public static String getSuperAdminPassword(){
		init();
		return superPassword;
	}
	
	public synchronized static void init(){
		if(isInited){
			return;
		}
		try {
			Document doc = XMLUtils.loadDocument(UserHelper.class.getResourceAsStream(CONF_FILE));
			Element root = doc.getDocumentElement();
			Node adminNode = XMLUtils.getChildNodeOf(root, "administrator");
			String strId = XMLUtils.getAtrributeValueOf(adminNode, "id");
			superAdminId = Long.parseLong(strId);
			superAdmincode = XMLUtils.getAtrributeValueOf(adminNode, "code");
			superAdminName = XMLUtils.getAtrributeValueOf(adminNode, "name");
			superPassword = XMLUtils.getAtrributeValueOf(adminNode, "password");
			
			Node localNode = XMLUtils.getChildNodeOf(root, "e3corp");
			strId = XMLUtils.getAtrributeValueOf(localNode, "id");
			localCorpId = Long.parseLong(strId);
			localCorpCode = XMLUtils.getAtrributeValueOf(localNode, "code");
			localCorpName = XMLUtils.getAtrributeValueOf(localNode, "name");
			
			isInited = true;
		} catch (Exception e) {
			isInited = false;
			e.printStackTrace();
			System.err.print("Load lifecycle listener configuration file failed");
		}

	}
		
}
